﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;
using System.Data;

namespace TimKen.Web.Admin.Recommend {
    public partial class Recommend_Add : AdminPage {

        private BussLogic.Recommend recommend = new TimKen.BussLogic.Recommend();
        private PCategoryManager categoryManager = new PCategoryManager();
        private string toadd = "├";
        public int RecommendID {
            get {
                if (ViewState["RecommendID"] != null)
                    return Convert.ToInt32(ViewState["RecommendID"]);
                else
                    return 0;
            }
            set {
                ViewState["RecommendID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                //绑定产品分类
                this.ddlCategory.Items.Clear();
                this.ddlCategory.Items.Add(new ListItem(GetLangString("ChooseCategory"), "-1"));

                BindResources();
                BindProductCategory("0");

                if (Request.QueryString["RecommendID"] != null) {
                    RecommendID = Convert.ToInt32(Request.QueryString["RecommendID"]);
                }
                if (RecommendID != 0)
                    BindData();
            }
        }

        /// <summary>
        /// 绑定产品分类分级选择列表
        /// </summary>
        private void BindProductCategory(string parentId) {
            DataSet ds = categoryManager.GetList(LanguageWhereStr + " AND ParentID=" + parentId);
            if (ds.Tables[0].Rows.Count > 0) {
                foreach (DataRow dr in ds.Tables[0].Rows) {
                    this.ddlCategory.Items.Add(new ListItem(toadd + " " + dr["CategoryName"].ToString(), dr["CategoryID"].ToString()));
                    toadd += "─┴";
                    BindProductCategory(dr["CategoryID"].ToString());
                    toadd = toadd.Substring(0, toadd.Length - 2);
                }
            }
        }



        private void BindData() {
            recommend = new TimKen.BussLogic.Recommend(RecommendID);

            if (this.ddlCategory.SelectedItem != null)
                this.ddlCategory.SelectedItem.Selected = false;

            foreach (ListItem item in this.ddlCategory.Items) {
                if (item.Value == recommend.CategoryID.ToString()) {
                    item.Selected = true; break;
                }
            }
            this.fckDescriptionCn.Value = recommend.DescriptionCn;
            this.fckDescriptionEn.Value = recommend.DescriptionEn;
            this.txtLogo.Text = recommend.Image;

            if (!string.IsNullOrEmpty(recommend.Image))
                this.imgLogoImage.ImageUrl = UploadFilePathForProduct + recommend.Image;
            else
                this.imgLogoImage.Visible = false;
        }

        private void BindResources() {
            this.btnSave.Text = GetLangString("Save");

        }
       

        protected void btnSave_Click(object sender, EventArgs e) {

            recommend.CategoryID = int.Parse(this.ddlCategory.SelectedValue);
            if (recommend.CategoryID == -1)
                throw new Exception("选择推荐分类");
            recommend.DescriptionCn = this.fckDescriptionCn.Value;
            recommend.DescriptionEn = this.fckDescriptionEn.Value;
            recommend.Image = this.txtLogo.Text;
            recommend.CreateDate = DateTime.Now;
            recommend.RecommendID = RecommendID;
            if (RecommendID != 0) {
                recommend.Update();
            } else {
                recommend.Add();
            }

            Response.Redirect("Recommend_Manage.aspx");
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e) {
            if (this.txtLogo.Text != "") {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(PhysicalUploadFilePathForProduct + this.txtLogo.Text);
                if (fileInfo.Exists) {
                    try {
                        fileInfo.Delete();

                        this.imgLogoImage.Visible = false;
                        this.imgLogoImage.ImageUrl = "";
                        this.txtLogo.Text = "";
                    } catch (Exception ex) {
                        throw new Exception("删除失败,错误信息" + ex.Message);
                    }
                }

            }
        }


        protected void btnRefresh_Click(object sender, EventArgs e) {

            this.imgLogoImage.Visible = true;
            this.imgLogoImage.ImageUrl = UploadFilePathForProduct + this.txtLogo.Text;



        }
    }
}
